﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utilities;

namespace StockCommon
{
    public class IndexGroupDef
    {
        public Guid id;
        public String 名稱 { get; set; }
        public String realName;
        public String 市場 { get; set; }
        
    }
    public class MinuteIndexRecord
    {
        public String 名稱 { get; set; }
        public String code { get; set; }
        public DateTime date { get; set; }
        public List<MinuteIndex> records { get; set; }
        public Boolean hasVolume { get; set; }
        public Guid id;
        public    MinuteIndexRecord()
        {
            code="";
            hasVolume = false;
        }
    }
    public class MinuteIndex
    {
        public  DateTime time { get; set; }
        public Double value { get; set; }
        public Double volume { get; set; }
    }

    public class StockIndexGroupManager
    {

        static List<IndexGroupDef> 上市每一分鐘指數名稱列表 = null;
        static int preRecordCount = -1;
        static List<DateTime> 上市每一分鐘指數日期列表 = null;

        public static Boolean hasMinuteTseData(DateTime  date)
        {
            if (上市每一分鐘指數日期列表 == null)
                getValidDayOfMinutesTseIndex();

            if (上市每一分鐘指數日期列表.Contains(date))
                return true;
            return false;
        }

        protected IndexGroupDef getMinuteTseIndexGroupDef(Guid id)
        {
            getMinutesTseIndexNames();

            foreach (IndexGroupDef r in 上市每一分鐘指數名稱列表)
            {
                if (r.id.Equals(id))
                    return r;
            }
            return null;

        }
        public static List<DateTime> getValidDayOfMinutesTseIndex()
        {
            StockDataClassesDataContext context = LinqAgent.getContext();
            int c=context.ExecuteQuery<int>("select COUNT(*) as c from WeightedIndexOneMinute" ).FirstOrDefault<int>();
            if (c == preRecordCount)
            {
                return 上市每一分鐘指數日期列表;
            }
            preRecordCount = c;
            上市每一分鐘指數日期列表=context.ExecuteQuery<DateTime>("select distinct [day] from WeightedIndexOneMinute order by [day]").ToList<DateTime>();
            return 上市每一分鐘指數日期列表;
        }
        public static List<IndexGroupDef> getMinutesTseIndexNames()
        {
            if (上市每一分鐘指數名稱列表 != null)
                return 上市每一分鐘指數名稱列表;

            StockDataClassesDataContext context = LinqAgent.getContext();
            var ll = (from r in context.WeightedIndexes select r);
            List<IndexGroupDef> result = new List<IndexGroupDef>();
            foreach (WeightedIndex r in ll)
            {
                if("tse".Equals(r.交易市場)==false)
                    continue;
                if (r.指數名稱.StartsWith("每一分鐘") == false)
                    continue;

                IndexGroupDef rec = new IndexGroupDef();
                rec.id = (Guid)r.id;
                rec.realName = r.指數名稱;
                rec.市場 = r.交易市場;
                String s = (String)rec.名稱;
                s = s.Replace("上市每一分鐘", "");
                s = s.Replace("類指數", "");
                s = s.Replace("類", "");
                rec.名稱 = s;
                result.Add(rec);
            }
            上市每一分鐘指數名稱列表 = result;
            return result;
        }


        public MinuteIndexRecord getMinuteTseOfDay(String name, DateTime date)
        {
            List<IndexGroupDef> ll = getMinutesTseIndexNames();
            foreach (IndexGroupDef r in ll)
            {
                if (r.名稱.Contains(name) || r.realName.Contains(name))
                {
                    return getMinuteTseOfDay(r.id, date);
                }
            }
            return null;
        }
        public MinuteIndexRecord getMinuteTseOfDay(Guid id, DateTime date)
        {

            StockDataClassesDataContext context = LinqAgent.getContext();
            date = DateUtils.getDateOnly(date);
            var ll=(from r in context.WeightedIndexOneMinutes where (r.day == date && r.stockId == id) orderby r.time ascending select r);
            if (ll.Count() == 0)
                return null;

            IndexGroupDef def = getMinuteTseIndexGroupDef(id);
            MinuteIndexRecord result = new MinuteIndexRecord();
            result.名稱 = def.名稱;
            result.id = def.id;
            result.hasVolume = false;
            result.date = date;
            result.code = "";

            List<MinuteIndex> records = new List<MinuteIndex>();
            result.records = records;

            foreach (WeightedIndexOneMinute r in ll)
            {
                MinuteIndex rr=new MinuteIndex();
                rr.time =(DateTime) r.time;
                rr.value = (Double)r.value;
                records.Add(rr);
            }
            return result;
        }


    }
}
